****************************
* Replication commands for "The Effects of Weather-Induced Migration on Sons of the Soil Violence in India", Bhavnani and Lacina, World Politics 2015
****************************

use "BhavnaniLacina2015replication1.dta", clear
notes

*****
* Global macros
*****
global controlspretreat 		ADEprecip_avg Lpdegrade_avg Lincome_avg Lunemploy_malenatsec Lpcprod Lpop_avgnat Lurbannat Lschoolmnat Lybulgenat 
global controlsexceptunemploy 	ADEprecip_avg Lpdegrade_avg Lincome_avg 				     Lpcprod Lpop_avgnat Lurbannat Lschoolmnat Lybulgenat 

*******
* Install program ivreg2
*******
*Will install if not already installed:
capture ssc install ivreg2
*Check for updates if already installed:
capture update ivreg2, update

********
*FIGURES
********

***********
*Figure 1: First stage relationship
***********
lpoly  Losm_m_avg LPDW_ADEprecip

***********
*Figure 2
***********
lpoly Lriots_cii_avg Losm_m_avg

***********
*Figure 3
***********
preserve
gen where=-.4
gen pipe="|"
summ polmatch_avg
global max = r(max)
global min = r(min)
qui xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpol=	LPDW_ADEprecip polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)
mat V=e(V)
local var1=V[1,1]
local var2=V[2,2]
local covar=V[2,1]
twoway (function y = _b[Losm_m_avg] + _b[Losm_mxpolmatch]*x, range($min $max)) ///
(function y = _b[Losm_m_avg] + _b[Losm_mxpolmatch]*x + 1.645*(`var1'+x^2*`var2'+2*x*`covar')^0.5, range($min $max) lpattern("-")) ///
(function y = _b[Losm_m_avg] + _b[Losm_mxpolmatch]*x - 1.645*(`var1'+x^2*`var2'+2*x*`covar')^0.5, range($min $max) lpattern("-")) ///
(scatter where polmatch_avg, ms(none) mlabel(pipe) mlabpos(0) legend(off)), ///
ytitle("% Change in riots per" "% change in male migration",size(large)) xtitle("Center-state political match",size(large))
restore

***********
*Figure 4 (Appendix)
***********
preserve
	quietly xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname , first robust bw(2)
	keep if e(sample)
	*first stage--Losm_m_avg
	qui xi: reg Losm_m_avg LPDW_ADEprecip   $controlspretreat i.stname   [aweight=yrswght]
	capture drop prLosm_m
	predict prLosm_m
	*E(Y|X)
	qui xi: reg Lriots_cii_avg   $controlspretreat i.stname   [aweight=yrswght]
	capture drop eyx
	predict eyx, resid	
	*E(Migration|X)
	qui xi: reg prLosm_m   $controlspretreat i.stname   [aweight=yrswght]
	capture drop emalemigx
	predict emalemigx, resid	

	label var emalemigx "Ln male migrants | X"
	label var eyx "Ln Riots | X"
	twoway (lfit eyx emalemigx, lc(black) )(scatter eyx emalemigx, msymbol(smcircle) mcolor(black)), ytitle("Ln Riots | X") legend(off) title("") note("") ylabel(,nogrid)
restore
capture window manage close graph _all 

****************
* Table 1: Summary stats
****************
summ Losm_m_avg Lriots_cii_avg Lunemploy_malenatsec polmatch_avg LPDW_ADEprecip $controlsexceptunemploy 

*****************
* Table 2: No interactions (results also displayed in Table 7 in online supplement)
*****************
newey Lriots_cii_avg Losm_m_avg [aweight=yrswght] ,  lag(2) force
xi: newey Lriots_cii_avg Losm_m_avg $controlspretreat i.stname    [aweight=yrswght] ,  lag(2) force
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*************************
* Table 3: Interaction w unemployment (results also displayed in Table 8 in online supplement)
*************************
xi: newey Lriots_cii_avg Losm_m_avg Losm_mxLumalenatsec $controlspretreat i.stname [aweight=yrswght] ,  lag(2) force
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*************************
* Table 4: Interaction w pol (results also displayed in Table 9 in online supplement)
*************************
xi: newey Lriots_cii_avg Losm_m_avg Losm_mxpolmatch polmatch_avg $controlspretreat i.stname [aweight=yrswght],  lag(2) force
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

******************
* Appendix tables
******************

******************
* Table 11: Summary stats for appendix variables
******************
summ LPDW_Astdprecipabs Astdprecipabs LPDW_ADEyrprecip ADEyrprecip LPosm_m Losm_all_avg ///
Losm_u_m_avg Lunemploy_urbanmalenat Lunemploy_natsec Lunemploy_malenatillit Lunemploy_malenatprim Losm_r_m_avg Lunemploy_ruralmalenat ///
LPDW_riots_cii_avg Lefnoparties_avg Lwinmargin_avg Lnetpop Lmigstock_male reside_mnat sa_affectpop_avg LntnlPW_ADEmonsoon ///
Lmurder_avg LPDWL_ADEprecip LPDWH_ADEprecip polmatchIV_avg 

***********************
* Table 12: Political match as determinant migration
***********************
xi: newey Losm_m_avg polmatch_avg Lriots_cii_avg i.stname [aweight=yrswght], lag(2) force

*******************
* Table 13: Comparing instruments
*******************

foreach instr in Astdprecipabs ADEyrprecip ADEprecip {

xi: newey Losm_m_avg LPDW_`instr' `instr'_avg Lpdegrade_avg Lincome_avg Lunemploy_malenatsec Lpcprod Lpop_avgnat Lurbannat Lschoolmnat Lybulgenat i.stname [aweight=yrswght] , lag(2) force

}

*********
* Tables 14-16: Standardize components of interaction terms to enable comparisons 
*********
preserve

foreach x of varlist Losm_m_avg polmatch_avg $controlspretreat LPDW_ADEprecip {
egen S`x'=std(`x')
drop `x'
rename S`x' `x'
}

drop Losm_mxLumalenatsec Losm_mxp Lun_malenatsecxLPDW_ADEprecip polmatch_avgxLPDW_ADEprecip
gen Losm_mxLumalenatsec=Losm_m_avg*Lunemploy_malenatsec 
gen Losm_mxpolmatch=Losm_m_avg*polmatch_avg
gen polmatch_avgxLPDW_ADEprecip=LPDW_ADEprecip*polmatch_avg
gen Lun_malenatsecxLPDW_ADEprecip=LPDW_ADEprecip*Lunemploy_malenatsec 

*Table 14
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 15
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 16
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

restore

*****************
* Tables 17-19: Unweighted data
*****************

*Table 17
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname , first robust bw(2)

*Table 18
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname, first robust bw(2)

*Table 19
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname, first robust bw(2)

****************
* Dropping cases: Tables 20-22
****************

forval x = 2(1)7 {

di ""
di "Redo results in Table 2"
di "Dropped period = `x'"
di ""
di ""
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] if period!=`x', savefirst robust bw(2)

di ""
di "Redo results in Table 3"
di "Dropped period = `x'"
di ""
di ""
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] if period!=`x', savefirst robust bw(2)

di ""
di "Redo results in Table 4"
di "Dropped period = `x'"
di ""
di ""
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght] if period!=`x', savefirst robust bw(2)

}

foreach x in 1 2 3 4 5 8 9 10 11 14 15 16 18 20 21 23 27 28 29 31 32 33 34 25 36 37 {

di ""
di "Redo results in Table 2"
di "Dropped state = `x'"
di ""
di ""
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] if statenm!=`x', savefirst robust bw(2)

di ""
di "Redo results in Table 3"
di "Dropped state = `x'"
di ""
di ""
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] if statenm!=`x', savefirst robust bw(2)

di ""
di "Redo results in Table 4"
di "Dropped state = `x'"
di ""
di ""
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght] if statenm!=`x', savefirst robust bw(2)


}

**************
* Tables 23-25: Winsorizing instrument, DV, IV
**************
preserve
foreach var of varlist Lriots_cii_avg LPDW_ADEprecip polmatch_avg Losm_m_avg Lunemploy_malenatsec {

qui summ `var', d

local mymin=r(p10)

local mymax=r(p90)

replace `var'=`mymin' if `var'<`mymin'

replace `var'=`mymax' if `var'>`mymax'

}

replace Losm_mxLumalenatsec=Losm_m_avg*Lunemploy_malenatsec

replace Lun_malenatsecxLPDW_ADEprecip=LPDW_ADEprecip*Lunemploy_malenatsec

replace Losm_mxp=Losm_m_avg*polmatch_avg

replace polmatch_avgxLPDW_ADEprecip=LPDW_ADEprecip*polmatch_avg

*Table 23
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 24
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 25
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

restore

*************
* Tables 26-28: Male migrants as a percent of the population
*************

*Table 26
xi: ivreg2 Lriots_cii_avg (LPosm_m=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 27
xi: ivreg2 Lriots_cii_avg (LPosm_m LPosm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first  robust bw(2)

*Table 28
xi: ivreg2 Lriots_cii_avg (LPosm_m LPosm_mxpolmatch = LPDW_ADEprecip  polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*************
* Tables 29-31: All migrants
*************

*Table 29
xi: ivreg2 Lriots_cii_avg (Losm_all_avg=LPDW_ADEprecip) Lunemploy_natsec $controlsexceptunemploy i.stname [aweight=yrswght], first  robust bw(2)

*Table 30
xi: ivreg2 Lriots_cii_avg (Losm_all_avg Losm_axLunatsec = LPDW_ADEprecip Lunemploy_natsecxLPDW_ADEprecip) Lunemploy_natsec $controlsexceptunemploy i.stname [aweight=yrswght] , first  robust bw(2)

*Table 31
xi: ivreg2 Lriots_cii_avg (Losm_all_avg Losm_axpolmatch = LPDW_ADEprecip polmatch_avgxLPDW_ADEprecip) polmatch_avg  Lunemploy_natsec $controlsexceptunemploy i.stname [aweight=yrswght] , first robust bw(2)

****************
* Tables 32-35: Alternative measures of unemployment
****************

*Table 32
xi: ivreg2 Lriots_cii_avg (Losm_u_m_avg  Losm_uxLuumalenatsec = LPDW_ADEprecip Lun_urbanmalenatxLPDW_ADEprecip) Lunemploy_urbanmalenat $controlsexceptunemploy i.stname [aweight=yrswght] , first  robust bw(2)

*Table 33
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLun_malenatprim = LPDW_ADEprecip Lun_malenatprimxLPDW_ADEprecip) Lunemploy_malenatprim $controlsexceptunemploy i.stname [aweight=yrswght] , first  robust bw(2)

*Table 34
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLun_malenatillit = LPDW_ADEprecip Lun_malenatillitxLPDW_ADEprecip) Lunemploy_malenatillit $controlsexceptunemploy i.stname [aweight=yrswght] , first  robust bw(2)

*Table 35
xi: ivreg2 Lriots_cii_avg (Losm_r_m_avg Losm_rmxLun_ruralmalenat = LPDW_ADEprecip  Lu_ruralmalenatxLPDW_ADEprecip)  Lunemploy_ruralmalenat $controlsexceptunemploy i.stname [aweight=yrswght] , first  robust bw(2)

***************
* Tables 36-38: Additional control variables
***************

*Table 36
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) LPDW_riots_cii_avg Lefnoparties_avg Lwinmargin_avg Lnetpop Lmigstock_male reside_mnat sa_affectpop_avg LntnlPW_ADEmonsoon $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

*Table 37
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) LPDW_riots_cii_avg Lefnoparties_avg Lwinmargin_avg Lnetpop Lmigstock_male reside_mnat sa_affectpop_avg LntnlPW_ADEmonsoon $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

*Table 38
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip polmatch_avgxLPDW_ADEprecip) polmatch_avg LPDW_riots_cii_avg Lefnoparties_avg Lwinmargin_avg Lnetpop Lmigstock_male reside_mnat sa_affectpop_avg LntnlPW_ADEmonsoon $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

*************
* Tables 39-41: Homicides as DV
*************

*Table 39
xi: ivreg2 Lmurder_avg  (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

*Table 40
xi: ivreg2 Lmurder_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

*Table 41
xi: ivreg2 Lmurder_avg  (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

***************
* Tables 42-43: Language weighted monsoon instrument
***************

*Table 42
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDWL_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 43
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDWL_ADEprecip Lun_malenatsecxLPDWL_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 44
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDWL_ADEprecip   polmatch_avgxLPDWL_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

************
* Tables 45-47: Fistorial migration rate weighted monsoon instrument
************
*Table 45
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDWH_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 46
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDWH_ADEprecip Lun_malenatsecxLPDWH_ADEprecip) $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 47
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDWH_ADEprecip   polmatch_avgxLPDWH_ADEprecip) polmatch_avg $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

************
* Table 48-50: Instrument using standardized monsoon rainfall
************
*Table 48
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_Astdprecipabs) Astdprecipabs_avg Lpdegrade_avg Lincome_avg Lunemploy_malenatsec Lpcprod Lpop_avgnat Lurbannat Lschoolmnat Lybulgenat  i.stname [aweight=yrswght], first robust bw(2)

*Table 49
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_Astdprecipabs  Lun_malenatsecxLPDW_Astdp) Astdprecipabs_avg Lpdegrade_avg Lincome_avg Lunemploy_malenatsec Lpcprod Lpop_avgnat Lurbannat Lschoolmnat Lybulgenat  i.stname [aweight=yrswght], first robust bw(2)

*Table 50
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_Astdprecipabs  polmatch_avgxLPDW_Astdprecipabs) polmatch_avg Astdprecipabs_avg Lpdegrade_avg Lincome_avg Lunemploy_malenatsec Lpcprod Lpop_avgnat Lurbannat Lschoolmnat Lybulgenat  i.stname [aweight=yrswght], first robust bw(2)

********
* Table 51: Adding an instrument for political alignment
********
xi: ivreg2  Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch polmatch_avg=polmatchIV_avg LPDW_ADEprecip polmatchIV_avgxLPDW_ADEprecip) $controlspretreat i.stname [aweight=yrswght], first r bw(2)

***********
* Tables 52-54: Period dummies
**********

*Table 52
xi: newey Lriots_cii_avg Losm_m_avg $controlspretreat i.stname i.period,  lag(2) force
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) $controlspretreat i.stname i.period , first robust bw(2)

*Table 53
xi: newey Lriots_cii_avg Losm_m_avg Losm_mxLumalenatsec $controlspretreat i.stname i.period,  lag(2) force
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) $controlspretreat i.stname i.period, first robust bw(2)

*Table 54
xi: newey Lriots_cii_avg Losm_m_avg Losm_mxpolmatch $controlspretreat i.stname i.period,  lag(2) force
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg $controlspretreat i.stname i.period, first robust bw(2)

************
* Tables 55-57: Controlling for national trends in rainfall
************

*Table 55
xi: ivreg2 Lriots_cii_avg (Losm_m_avg=LPDW_ADEprecip) LntnlPW_ADEmonsoon $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 56
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxLumalenatsec = LPDW_ADEprecip Lun_malenatsecxLPDW_ADEprecip) LntnlPW_ADEmonsoon $controlspretreat i.stname [aweight=yrswght] , first robust bw(2)

*Table 57
xi: ivreg2 Lriots_cii_avg (Losm_m_avg Losm_mxpolmatch = LPDW_ADEprecip   polmatch_avgxLPDW_ADEprecip) polmatch_avg LntnlPW_ADEmonsoon $controlspretreat i.stname [aweight=yrswght], first robust bw(2)

***********
* Tables 5 & 6: Analysis of President's Rule
***********
*load second replication dataset
use "BhavnaniLacina2015replication2.dta", clear
notes

*Table 5
logit presruleonset Lpolmatch Lriotslag yrs* i.stateid

*Table 6
summ presruleonset Lpolmatch Lriotslag yrs*1 if e(sample)

